package com.ruoyi.web.controller.website;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.module.vo.ExamVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.module.domain.ExamFraction;
import com.ruoyi.module.service.IExamFractionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试分数Controller
 *
 * @author fubojin
 * @date 2025-03-05
 */
@RestController
@RequestMapping("/website/examFraction")
public class WebExamFractionController extends BaseController {
    @Autowired
    private IExamFractionService examFractionService;

    /**
     * 查询考试分数列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ExamFraction examFraction) {
        startPage();
        List<ExamFraction> list = examFractionService.selectExamFractionList(examFraction);
        return getDataTable(list);
    }

    /**
     * 获取考试次数详细信息
     */
    @GetMapping(value = "/userId")
    public AjaxResult getInfoByUserId(@RequestParam Long userId) {
        ExamVo examVo = new ExamVo();

        ExamFraction examFraction = new ExamFraction();
        examFraction.setUserId(userId);
        examFraction.setType(1);
        List<ExamFraction> examFractions1 = examFractionService.selectExamFractionList(examFraction);
        examVo.setJuniorExamNumber(examFractions1.size());
        examVo.setJuniorFraction(examFractions1.stream()
                .mapToDouble(ExamFraction::getFraction) // 提取分数字段
                .max() // 找到最大值
                .orElse(0.0)); // 如果列表为空，返回默认值 0.0


        examFraction.setType(2);
        List<ExamFraction> examFractions2 = examFractionService.selectExamFractionList(examFraction);
        examVo.setSeniorExamNumber(examFractions2.size());
        examVo.setSeniorFraction(examFractions2.stream()
                .mapToDouble(ExamFraction::getFraction) // 提取分数字段
                .max() // 找到最大值
                .orElse(0.0)); // 如果列表为空，返回默认值 0.0

        return success(examVo);
    }


    /**
     * 导出考试分数列表
     */
    @Log(title = "考试分数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamFraction examFraction) {
        List<ExamFraction> list = examFractionService.selectExamFractionList(examFraction);
        ExcelUtil<ExamFraction> util = new ExcelUtil<ExamFraction>(ExamFraction.class);
        util.exportExcel(response, list, "考试分数数据");
    }

    /**
     * 获取考试分数详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(examFractionService.selectExamFractionById(id));
    }

    /**
     * 新增考试分数
     */
    @Log(title = "考试分数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamFraction examFraction) {
        return toAjax(examFractionService.insertExamFraction(examFraction));
    }

    /**
     * 修改考试分数
     */
    @Log(title = "考试分数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamFraction examFraction) {
        return toAjax(examFractionService.updateExamFraction(examFraction));
    }

    /**
     * 删除考试分数
     */
    @Log(title = "考试分数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examFractionService.deleteExamFractionByIds(ids));
    }


}
